# -*- coding:utf-8 -*-
# !/usr/bin/python

import os
import sys
import shutil
import datetime
import time
import serial    
import thread

def printusage():
    print "Usage:"
    print "     sudo python *.py serialnumber baud interval_time"
    return;

def sendMessage2BLEandGetResponse(ser,msg,res=0):
    n = ser.write(msg)    
    print "Send: %d %s" % (n,msg),
    r = ser.readall()
    if len(r) == 0:
        print "No Response\n",
    else:
        print "Get : %s" % (r),
    if res == 0:
        return r;
    if r == res:
        return 1;
    else:
        return 0;

sn = 0
interval_time = 0.3
usradvdata = [0x07,0x08,0x4D,0x54,0x31,0x30,0x32,0x42,0x16,0xFF,0xFF,0x02,0x01,0x0F,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17]

def serialRX(threadName,ser):
    while 1:
        serialrxbuf = ser.readall()
        print serialrxbuf

def serialTx(threadName,ser):
    while 1:
        print "Time: %s.%d" % (time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())),datetime.datetime.now().microsecond)
        workstyle = sendMessage2BLEandGetResponse(ser,'at+bleWorkstyle?\r\n')[13:14]
        consta = sendMessage2BLEandGetResponse(ser,'at+consta?\r\n')[7:9]
        
        if workstyle == '3' and consta != 'FF':
            sendMessage2BLEandGetResponse(ser,'%d' % sn + '=====>------>asfsjghajsdghasjghdajgdiiiieryerharehasdhadhadfjhad,jfghadfjg4536376917967261haidsjasfgasdhasdghadsjg!@#$%^&**(()_+-=gghdhdhhuyyweyyw6468sjdfj)1234567890\r\n')
            sn = sn + 1
        elif workstyle == '3' and consta == 'FF':
            sn = 0
            sendMessage2BLEandGetResponse(ser,usradvdata)
            usradvdata[14] = (usradvdata[14] + 1) & 0xff
            time.sleep(interval_time/1000.0) 
        print "\r\n"


if __name__ == '__main__':

    if(len(sys.argv) < 3):
        printusage()
        exit()
    if(len(sys.argv) < 4):
        interval_time = 300
    else:
        interval_time = int(sys.argv[3])
    ser = serial.Serial(sys.argv[1],int(sys.argv[2]),timeout=0.015)    
    sendMessage2BLEandGetResponse(ser,'at+\r\n','OK\r\n')
    sendMessage2BLEandGetResponse(ser,'at+maC?\r\n')
    sendMessage2BLEandGetResponse(ser,'AT+vErsion?\r\n','OK\r\n')
    workstyle = sendMessage2BLEandGetResponse(ser,'at+bleWorkstyle?\r\n')[16:17]
    consta = sendMessage2BLEandGetResponse(ser,'at+consta?\r\n')[10:12]

    while 1:
        print "Time: %s.%d" % (time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())),datetime.datetime.now().microsecond)
        #ser = serial.Serial(sys.argv[1],int(sys.argv[2]),timeout=0.015)    
        #sendMessage2BLEandGetResponse(ser,'at+\r\n','OK\r\n')
        #sendMessage2BLEandGetResponse(ser,'at+maC?\r\n')
        #sendMessage2BLEandGetResponse(ser,'AT+vErsion?\r\n','OK\r\n')
        workstyle = sendMessage2BLEandGetResponse(ser,'at+bleWorkstyle?\r\n')[16:17]
        consta = sendMessage2BLEandGetResponse(ser,'at+consta?\r\n')[10:12]
        #sendMessage2BLEandGetResponse(ser,'asfsjghajsdghasjghdajgdha;dsjghadsjg\r\n')
        
        if workstyle == '3' and consta != 'FF':
            sendMessage2BLEandGetResponse(ser,'%d' % sn + '=====>------>asfsjghajsdghasjghdajgdiiiieryerharehasdhadhadfjhad,jfghadfjg4536376917967261haidsjasfgasdhasdghadsjg!@#$%^&**(()_+-=gghdhdhhuyyweyyw6468sjdfj)1234567890\r\n')
            sn = sn + 1
        elif workstyle == '3' and consta == 'FF':
            sn = 0
            sendMessage2BLEandGetResponse(ser,usradvdata)
            usradvdata[14] = (usradvdata[14] + 1) & 0xff
            time.sleep(interval_time/1000.0) 

        print "\r\n"

